//
// Created by 施旗坤 on 2025/11/7.
//
#include "admin_select.h"
#include "admin_function.h"

int clear_buffer5() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
}
int category_id_select() {
    book_information b;
    printf("请输入要查询的图书种类ID:(科幻：1，文艺：2，经济：3)\n");
    printf("请输入选择（1-3）：");
    if (scanf("%d", &b.category_id) != 1 || b.category_id <= 0) {
        printf("输入错误：请输入有效的图书种类ID\n");
        clear_buffer5();
        return -1;
    }
    // 先查询种类ID对应的种类名称
    const char *get_category_name_sql = "select category_name from book_category where category_id = ?";
    sqlite3_stmt *name_stmt;
    int rc = sqlite3_prepare_v2(db, get_category_name_sql, -1, &name_stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "预编译查询种类名称语句出错: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_int(name_stmt, 1, b.category_id);
    const char *category_name = NULL;
    rc = sqlite3_step(name_stmt);
    if (rc == SQLITE_ROW) {
        category_name = (const char *)sqlite3_column_text(name_stmt, 0);
    }
    sqlite3_finalize(name_stmt);

    // 主查询：通过种类ID查询图书
    const char *select_id_sql = "select b.id, b.book_name, b.author, b.publish, "
                                "b.publish_date, bc.category_name, b.stock "
                                "from book b "
                                "join book_category bc on b.category_id = bc.category_id "
                                "where b.category_id = ? ";
    sqlite3_stmt *select_stmt;
    rc = sqlite3_prepare_v2(db, select_id_sql, -1, &select_stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "预编译搜索语句出错: %s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_int(select_stmt, 1, b.category_id);

    int row_count = 0;
    // 标题中显示种类名称（如果存在）
//    if (category_name) {
//        printf("\n======= 种类为《%s》的图书列表 =======\n", category_name);
//    } else {
//        printf("\n======= 种类ID为 %d 的图书列表 =======\n", b.category_id);
//    }

    rc = sqlite3_step(select_stmt);
    while (rc == SQLITE_ROW) {
        row_count++;
        int book_id = sqlite3_column_int(select_stmt, 0);
        const char *book_name = (const char *)sqlite3_column_text(select_stmt, 1);
        const char *author = (const char *)sqlite3_column_text(select_stmt, 2);
        const char *publish = (const char *)sqlite3_column_text(select_stmt, 3);
        const char *publish_date = (const char *)sqlite3_column_text(select_stmt, 4);
        const char *curr_category_name = (const char *)sqlite3_column_text(select_stmt, 5);
        int stock = sqlite3_column_int(select_stmt, 6);

        printf("第%d本:\n", row_count);
        printf("图书ID: %d\n", book_id);
        printf("图书名称: %s\n", book_name);
        printf("图书作者: %s\n", author);
        printf("图书出版社: %s\n", publish);
        printf("出版日期: %s\n", publish_date);
        printf("图书种类: %s\n", curr_category_name);  // 这里已输出种类名称
        printf("数量: %d\n", stock);
        printf("------------------------\n");
        rc = sqlite3_step(select_stmt);
    }

    if (rc != SQLITE_DONE) {
        fprintf(stderr, "搜索图书失败: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(select_stmt);
        return -1;
    }

    sqlite3_finalize(select_stmt);
    if (row_count == 0) {
        if (category_name) {
            printf("不存在种类为《%s》的图书\n", category_name);
        } else {
            printf("不存在种类ID为%d的图书\n", b.category_id);
        }
    } else {
        printf("共查询到 %d 本图书\n", row_count);
    }
    return 0;
}
int author_select(){
    book_information b;
    printf("请输入要查询的图书作者:\n");
    scanf("%s",b.author);
    const char * select_author_sql="select * from book where author =?";
    sqlite3_stmt * select_stmt;
    int rc= sqlite3_prepare_v2(db,select_author_sql,-1,&select_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译搜索语句出错%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(select_stmt,1,b.author,-1,SQLITE_TRANSIENT);
    rc= sqlite3_step(select_stmt);
    int row=0;
    while (rc==SQLITE_ROW) {
        row++;
        printf("图书ID: %d\n", sqlite3_column_int(select_stmt, 0));
        printf("图书名称: %s\n", (const char *)sqlite3_column_text(select_stmt, 1));
        printf("图书价格: %s\n", (const char *)sqlite3_column_text(select_stmt, 2));
        printf("出版社: %s\n", (const char *)sqlite3_column_text(select_stmt, 3));
        printf("作者: %s\n", (const char *)sqlite3_column_text(select_stmt, 4));
        printf("出版日期: %s\n", (const char *)sqlite3_column_text(select_stmt, 5));
        printf("数量: %s\n", (const char *)sqlite3_column_text(select_stmt, 6));
        rc= sqlite3_step(select_stmt);
    }
    if (row==0) {
        printf("不存在作者为%s的图书\n",b.author);
    }else if(row!=SQLITE_DONE){
        fprintf(stderr, "搜索图书失败%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(select_stmt);
        return -1;
    }
    sqlite3_finalize(select_stmt);
    return 0;
}
int name_select(){
    book_information b;
    printf("请输入要查询的图书名字:\n");
    scanf("%s",b.book_name);
    const char * select_sql="select * from book where book_name =?";
    sqlite3_stmt * select_stmt;
    int rc= sqlite3_prepare_v2(db,select_sql,-1,&select_stmt,NULL);
    if (rc!=SQLITE_OK){
        fprintf(stderr,"预编译搜索语句出错%s\n", sqlite3_errmsg(db));
        return -1;
    }
    sqlite3_bind_text(select_stmt,1,b.book_name,-1,SQLITE_TRANSIENT);
    rc= sqlite3_step(select_stmt);
    int row=0;
    while (rc==SQLITE_ROW){
        row++;
        printf("图书ID: %d\n", sqlite3_column_int(select_stmt, 0));
        printf("图书名称: %s\n", (const char *)sqlite3_column_text(select_stmt, 1));
        printf("图书价格: %s\n", (const char *)sqlite3_column_text(select_stmt, 2));
        printf("出版社: %s\n", (const char *)sqlite3_column_text(select_stmt, 3));
        printf("作者: %s\n", (const char *)sqlite3_column_text(select_stmt, 4));
        printf("出版日期: %s\n", (const char *)sqlite3_column_text(select_stmt, 5));
        printf("数量: %s\n", (const char *)sqlite3_column_text(select_stmt, 6));
        rc= sqlite3_step(select_stmt);
        }
    if (row==0) {
        printf("不存在图书名字为%s的图书\n", b.book_name);
    }
    else if(rc!=SQLITE_DONE){
        fprintf(stderr, "搜索图书失败%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(select_stmt);
        return -1;
    }
    sqlite3_finalize(select_stmt);
    return 0;
}